home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / CYCLOID2.FRM < prev    next >
Text File  |  1996-04-24  |  8KB  |  310 lines

  1. VERSION 4.00
  2. Begin VB.Form Cycloid2Form 
  3.    Caption         =   "Cycloid 2"
  4.    ClientHeight    =   5670
  5.    ClientLeft      =   2085
  6.    ClientTop       =   735
  7.    ClientWidth     =   4830
  8.    Height          =   6360
  9.    Left            =   2025
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   378
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Top             =   105
  15.    Width           =   4950
  16.    Begin VB.TextBox DtText 
  17.       Height          =   285
  18.       Left            =   240
  19.       TabIndex        =   11
  20.       Text            =   "0.025"
  21.       Top             =   45
  22.       Width           =   615
  23.    End
  24.    Begin VB.TextBox QText 
  25.       Height          =   285
  26.       Left            =   2280
  27.       TabIndex        =   10
  28.       Text            =   "7"
  29.       Top             =   45
  30.       Width           =   615
  31.    End
  32.    Begin VB.TextBox PText 
  33.       Height          =   285
  34.       Left            =   1320
  35.       TabIndex        =   9
  36.       Text            =   "20"
  37.       Top             =   45
  38.       Width           =   615
  39.    End
  40.    Begin VB.TextBox RText 
  41.       Height          =   285
  42.       Left            =   3285
  43.       TabIndex        =   8
  44.       Text            =   "15"
  45.       Top             =   45
  46.       Width           =   615
  47.    End
  48.    Begin VB.TextBox ThetaText 
  49.       Height          =   285
  50.       Left            =   4200
  51.       TabIndex        =   6
  52.       Text            =   "-45"
  53.       Top             =   480
  54.       Width           =   615
  55.    End
  56.    Begin VB.TextBox YscaleText 
  57.       Height          =   285
  58.       Left            =   2160
  59.       TabIndex        =   4
  60.       Text            =   "1.2"
  61.       Top             =   480
  62.       Width           =   615
  63.    End
  64.    Begin VB.TextBox XscaleText 
  65.       Height          =   285
  66.       Left            =   600
  67.       TabIndex        =   2
  68.       Text            =   "0.7"
  69.       Top             =   480
  70.       Width           =   615
  71.    End
  72.    Begin VB.CommandButton CmdGo 
  73.       Caption         =   "Go"
  74.       Default         =   -1  'True
  75.       Height          =   375
  76.       Left            =   4200
  77.       TabIndex        =   1
  78.       Top             =   0
  79.       Width           =   615
  80.    End
  81.    Begin VB.PictureBox Canvas 
  82.       AutoRedraw      =   -1  'True
  83.       Height          =   4815
  84.       Left            =   0
  85.       ScaleHeight     =   -2.2
  86.       ScaleLeft       =   -1.1
  87.       ScaleMode       =   0  'User
  88.       ScaleTop        =   1.1
  89.       ScaleWidth      =   2.2
  90.       TabIndex        =   0
  91.       Top             =   840
  92.       Width           =   4815
  93.    End
  94.    Begin VB.Label Label1 
  95.       Caption         =   "dt"
  96.       Height          =   255
  97.       Index           =   6
  98.       Left            =   0
  99.       TabIndex        =   15
  100.       Top             =   60
  101.       Width           =   255
  102.    End
  103.    Begin VB.Label Label1 
  104.       Caption         =   "Q"
  105.       Height          =   255
  106.       Index           =   4
  107.       Left            =   2115
  108.       TabIndex        =   14
  109.       Top             =   60
  110.       Width           =   255
  111.    End
  112.    Begin VB.Label Label1 
  113.       Caption         =   "P"
  114.       Height          =   255
  115.       Index           =   1
  116.       Left            =   1200
  117.       TabIndex        =   13
  118.       Top             =   60
  119.       Width           =   255
  120.    End
  121.    Begin VB.Label Label1 
  122.       Caption         =   "R"
  123.       Height          =   255
  124.       Index           =   0
  125.       Left            =   3120
  126.       TabIndex        =   12
  127.       Top             =   60
  128.       Width           =   255
  129.    End
  130.    Begin VB.Label Label1 
  131.       Caption         =   "Angle (degrees)"
  132.       Height          =   255
  133.       Index           =   5
  134.       Left            =   3000
  135.       TabIndex        =   7
  136.       Top             =   525
  137.       Width           =   1215
  138.    End
  139.    Begin VB.Label Label1 
  140.       Caption         =   "Y scale"
  141.       Height          =   255
  142.       Index           =   3
  143.       Left            =   1560
  144.       TabIndex        =   5
  145.       Top             =   525
  146.       Width           =   615
  147.    End
  148.    Begin VB.Label Label1 
  149.       Caption         =   "X scale"
  150.       Height          =   255
  151.       Index           =   2
  152.       Left            =   0
  153.       TabIndex        =   3
  154.       Top             =   525
  155.       Width           =   615
  156.    End
  157.    Begin VB.Menu mnuFile 
  158.       Caption         =   "&File"
  159.       Begin VB.Menu mnuFileExit 
  160.          Caption         =   "E&xit"
  161.       End
  162.    End
  163. End
  164. Attribute VB_Name = "Cycloid2Form"
  165. Attribute VB_Creatable = False
  166. Attribute VB_Exposed = False
  167. Option Explicit
  168.  
  169. Const PI = 3.14159
  170. Const TWO_PI = 2 * PI
  171.  
  172. Dim P As Integer
  173. Dim Q As Integer
  174. Dim R As Integer
  175. Dim P_Q As Single
  176. Dim PQ As Integer
  177. Dim PQR As Integer
  178.  
  179.  
  180. ' ************************************************
  181. ' Draw the curve on the indicated picture box.
  182. ' ************************************************
  183. Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, Dt As Single, xscale As Single, yscale As Single, theta As Single)
  184. Dim x1 As Single
  185. Dim y1 As Single
  186. Dim x2 As Single
  187. Dim y2 As Single
  188. Dim ctheta As Single
  189. Dim stheta As Single
  190. Dim t As Single
  191.  
  192.     ' Save these values because we use them a lot.
  193.     stheta = Sin(theta)
  194.     ctheta = Cos(theta)
  195.     
  196.     x1 = xscale * X(start_t)
  197.     y1 = yscale * Y(start_t)
  198.     x2 = x1 * ctheta - y1 * stheta
  199.     y2 = x1 * stheta + y1 * ctheta
  200.     pic.Cls
  201.     pic.CurrentX = x2
  202.     pic.CurrentY = y2
  203.     
  204.     t = start_t + Dt
  205.     Do While t < stop_t
  206.         x1 = xscale * X(t)
  207.         y1 = yscale * Y(t)
  208.         x2 = x1 * ctheta - y1 * stheta
  209.         y2 = x1 * stheta + y1 * ctheta
  210.         pic.Line -(x2, y2)
  211.         t = t + Dt
  212.     Loop
  213.     
  214.     x1 = xscale * X(stop_t)
  215.     y1 = yscale * Y(stop_t)
  216.     x2 = x1 * ctheta - y1 * stheta
  217.     y2 = x1 * stheta + y1 * ctheta
  218.     pic.Line -(x2, y2)
  219. End Sub
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. ' ************************************************
  227. ' Non-recursively compute the greatest common
  228. ' divisor of to integers.
  229. ' ************************************************
  230. Private Function GCD(ByVal a As Integer, ByVal b As Integer) As Integer
  231. Dim B_Mod_A As Integer
  232.  
  233.     B_Mod_A = b Mod a
  234.     Do While B_Mod_A <> 0
  235.         ' Prepare the arguments for the "recursion."
  236.         b = a
  237.         a = B_Mod_A
  238.         B_Mod_A = b Mod a
  239.     Loop
  240.  
  241.     GCD = a
  242. End Function
  243.  
  244.  
  245. ' ************************************************
  246. ' Find the least common multiple of two integers.
  247. ' ************************************************
  248. Function LCM(a As Integer, b As Integer) As Integer
  249.     LCM = a * b / GCD(a, b)
  250. End Function
  251.  
  252.  
  253. ' ************************************************
  254. ' Calculate the values t must cross to draw a
  255. ' cycloid.
  256. ' ************************************************
  257. Sub SetTBounds(tmin As Single, tmax As Single)
  258.     tmin = 0
  259.     ' LCM / P * 2 * PI.
  260.     tmax = Q / GCD(P, Q) * TWO_PI
  261. End Sub
  262.  
  263.  
  264. ' ************************************************
  265. ' The parametric function X(t).
  266. ' ************************************************
  267. Function X(t As Single) As Single
  268.     X = (PQ * Cos(t) + R * Cos(t * P_Q)) / (PQR)
  269. End Function
  270.  
  271.  
  272. ' ************************************************
  273. ' The parametric function Y(t).
  274. ' ************************************************
  275. Function Y(t As Single) As Single
  276.     Y = (PQ * Sin(t) + R * Sin(t * P_Q)) / PQR
  277. End Function
  278.  
  279.  
  280. Private Sub CmdGo_Click()
  281. Dim tmin As Single
  282. Dim tmax As Single
  283. Dim Dt As Single
  284. Dim xscale As Single
  285. Dim yscale As Single
  286. Dim theta As Single
  287.  
  288.     P = CInt(PText.Text)
  289.     Q = CInt(QText.Text)
  290.     R = CInt(RText.Text)
  291.     P_Q = P / Q
  292.     PQ = P + Q
  293.     PQR = PQ + R
  294.     
  295.     SetTBounds tmin, tmax
  296.     
  297.     Dt = CSng(DtText.Text)
  298.     xscale = CSng(XscaleText.Text)
  299.     yscale = CSng(YscaleText.Text)
  300.     theta = CSng(ThetaText.Text) / 180 * PI
  301.     DrawCurve Canvas, tmin, tmax, Dt, xscale, yscale, theta
  302. End Sub
  303.  
  304.  
  305. Private Sub mnuFileExit_Click()
  306.     Unload Me
  307. End Sub
  308.  
  309.  
  310.